package com.lanyam.travel.base.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.utils.StringUtils;
import com.lanyam.travel.base.entity.GoodsDate;
import com.lanyam.travel.base.entity.GoodsInfo;
import com.lanyam.travel.base.entity.Product;
import com.lanyam.travel.base.service.IGoodsDateService;
import com.lanyam.travel.base.service.IGoodsInfoService;
import com.lanyam.travel.base.service.IScenicHotelService;
import com.lanyam.travel.base.service.ITravelLineService;
import com.lanyam.travel.base.utils.Constant;
import com.lanyam.travel.base.utils.DataStatisticsUtil;
import com.lanyam.travel.base.utils.JSONResult;

@RestController
@RequestMapping("date")
public class GoodsDateController {

	@Autowired
	private IGoodsDateService goodsDateService;
	
	@Autowired
	private ITravelLineService travelLineService;
	
	@Autowired
	private IScenicHotelService scenicHotelService;
	
	@Autowired
	private IGoodsInfoService goodsInfoService;
	
	/**
	 * 批量添加日历价格
	 * @param goodsId 线路id/门票id
	 * @param productType 产品类型，0为门票，2为线路，酒店单独处理，在goodsInfoController中处理
	 * @param goodsDateList 必填参数：price:价格；theDate：日期，格式2020/3/24，没有时分秒，remaindNum：票数量
	 * @return
	 */
	@PostMapping("/batchAddGoodsDate")
	public JSONResult batchAddGoodsDate(String goodsId,int productType,@RequestBody List<GoodsDate> goodsDateList)
	{
		if(StringUtils.isEmpty(goodsId)||StringUtils.isEmpty(productType+""))
		{
			return JSONResult.errorMsg("请求参数不全.");
		}
		Product product = null;
		//如果是酒店
		if(productType == 0)
		{
			product = scenicHotelService.getScenicById(goodsId);
		}
		else if(productType == 2)
		{
			product = travelLineService.getTravelLineById(goodsId);
		}
		else
		{
			return JSONResult.errorMsg("接口调用错误");
		}
		
		if(null == product) 
		{
			return JSONResult.errorMsg("获取商品信息失败");
		}
		List<GoodsDate> insertList = new ArrayList<GoodsDate>();
		//把商品日期表更新到一年半
		Calendar calendar = Calendar.getInstance();//获取当前日期0点的时候（00：00：00-24：00：00）
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		int days = Constant.TICKET_DAYS;
		GoodsDate goodsDate;
		outer:for (int i = 0; i < days; i++) {
			goodsDate = new GoodsDate();
			if(null != goodsDateList && goodsDateList.size() > 0)
			{
				for (GoodsDate date : goodsDateList)
				{
					if(DataStatisticsUtil.sameDate(calendar.getTime(), date.getTheDate()))
					{
						goodsDate.setGoodsId(goodsId);
						goodsDate.setPrice(date.getPrice());
						goodsDate.setPriceDisparity(0);
						goodsDate.setProductType(productType);
						goodsDate.setRemaindNum(date.getRemaindNum());
						goodsDate.setTheDate(date.getTheDate());
						insertList.add(goodsDate);
						continue outer;
					}
				}
			}
			//走到这里说明这个日期没有设置过，添加为默认参数
			goodsDate.setPrice(product.getPrice());
			goodsDate.setRemaindNum(product.getTotalNum());
			goodsDate.setGoodsId(goodsId);
			goodsDate.setPriceDisparity(0);
			goodsDate.setProductType(productType);
			goodsDate.setTheDate(calendar.getTime());//设置固定的时间为8：00：00
			insertList.add(goodsDate);
			calendar.add(Calendar.DAY_OF_YEAR, 1);
		}
		if(insertList.size() > 0)
		{
			goodsDateService.batchAddGoodsDates(insertList);
		}
		return JSONResult.ok();
	}
	/**
	 * 获取指定月份的日历
	 * @param month
	 * @param id 景区/线路id或酒店的房间类型id
	 * @param productType
	 * @return
	 */
	@PostMapping("/getGoodsDateByMonth")
	public JSONResult getGoodsDateByMonth(String month,String id,int productType)
	{
		if(StringUtils.isEmpty(month)||StringUtils.isEmpty(id)||StringUtils.isEmpty(productType+""))
		{
			return JSONResult.errorMsg("请求参数不全.");
		}
		Product product = null;
		GoodsInfo info = null;
		//如果是酒店
		if(productType == 1)
		{
			info = goodsInfoService.getGoodsInfoById(id);
		}
		else if(productType == 0)
		{
			product = scenicHotelService.getScenicById(id);
		}
		else
		{
			product = travelLineService.getTravelLineById(id);
		}
		
		if(null == info && null == product) 
		{
			return JSONResult.errorMsg("获取商品信息失败");
		}
		
		//把商品日期表更新到一年半
		Calendar calendar = Calendar.getInstance();//获取当前日期0点的时候（00：00：00-24：00：00）
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		Date latestDate = goodsDateService.getLatestDate(id);
		int days = Constant.TICKET_DAYS;
		Calendar afterCalendar;
		if(null != latestDate )
		{
			afterCalendar = Calendar.getInstance();
			afterCalendar.setTime(latestDate);//目标日期的时间点
			afterCalendar.set(Calendar.HOUR_OF_DAY, 0);
			afterCalendar.set(Calendar.MINUTE, 0);
			afterCalendar.set(Calendar.SECOND, 0);
			afterCalendar.set(Calendar.MILLISECOND, 0);
			days = (int)Math.ceil((afterCalendar.getTimeInMillis() - calendar.getTimeInMillis()) / (1000.0 * 60 * 60 * 24));
			days = Constant.TICKET_DAYS - 1 - days;
			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);//增加最新日期的下一天
			if(days<0)
			{
				days = 0;
			}
		}
		else
		{
			afterCalendar = calendar;
		}
		List<GoodsDate> addGoodsDateList = new ArrayList<GoodsDate>();
		GoodsDate goodsDate;
		for (int i = 0; i < days; i++) {
			goodsDate = new GoodsDate();
			goodsDate.setGoodsId(id);
			if(productType == 1)
			{
				goodsDate.setPrice(info.getDefaultPrice().doubleValue());
				goodsDate.setRemaindNum(info.getDefaultCount());
			}
			else
			{
				goodsDate.setPrice(product.getPrice());
				goodsDate.setRemaindNum(product.getTotalNum());
			}
			goodsDate.setPriceDisparity(0);
			goodsDate.setProductType(productType);
			goodsDate.setTheDate(new Timestamp(afterCalendar.getTimeInMillis()));//设置固定的时间为8：00：00
			System.out.println("时间为："+afterCalendar.getTime());
			addGoodsDateList.add(goodsDate);
			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);
		}
		if(addGoodsDateList.size() > 0)
		{
			goodsDateService.batchAddGoodsDates(addGoodsDateList);
		}
		List<GoodsDate> goodsDateInMouth = goodsDateService.queryGoodsDateByMouth(id,month);
		//查询当月的余票数
		return JSONResult.ok(goodsDateInMouth);
	}
	
	/**
	 * 获取未来一个月的剩余票
	 * @param id 景点/线路id或酒店中的商品id
	 * @param productType 0为景点;1为酒店;2为线路
	 * @return
	 */
	@PostMapping("/getRemaidGoodFromNow")
	@ResponseBody
	public JSONResult getRemaidGoodFromNow(String id,int productType)
	{
		if(StringUtils.isEmpty(id) || StringUtils.isEmpty(productType+""))
		{
			return JSONResult.errorMsg("请求参数不全.");
		}
		Product product = null;
		GoodsInfo info = null;
		//如果是酒店
		if(productType == 1)
		{
			info = goodsInfoService.getGoodsInfoById(id);
		}
		else if(productType == 0)
		{
			product = scenicHotelService.getScenicById(id);
		}
		else
		{
			product = travelLineService.getTravelLineById(id);
		}
		
		if(null == info && null == product) 
		{
			return JSONResult.errorMsg("获取商品信息失败");
		}
		
		//把商品日期表更新到30天
		Calendar calendar = Calendar.getInstance();//获取当前日期0点的时候（00：00：00-24：00：00）
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		Date latestDate = goodsDateService.getLatestDate(id);
		int days = Constant.TICKET_DAYS;
		Calendar afterCalendar;
		if(null != latestDate )
		{
			afterCalendar = Calendar.getInstance();
			afterCalendar.setTime(latestDate);//目标日期的时间点
			afterCalendar.set(Calendar.HOUR_OF_DAY, 0);
			afterCalendar.set(Calendar.MINUTE, 0);
			afterCalendar.set(Calendar.SECOND, 0);
			afterCalendar.set(Calendar.MILLISECOND, 0);
			days = (int)Math.ceil((afterCalendar.getTimeInMillis() - calendar.getTimeInMillis()) / (1000.0 * 60 * 60 * 24));
			days = Constant.TICKET_DAYS - 1 - days;
			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);//增加最新日期的下一天
			if(days<0)
			{
				days = 0;
			}
		}
		else
		{
			afterCalendar = calendar;
		}
		List<GoodsDate> addGoodsDateList = new ArrayList<GoodsDate>();
		GoodsDate goodsDate;
		for (int i = 0; i < days; i++) {
			goodsDate = new GoodsDate();
			goodsDate.setGoodsId(id);
			if(productType == 1)
			{
				goodsDate.setPrice(info.getDefaultPrice().doubleValue());
				goodsDate.setRemaindNum(info.getDefaultCount());
			}
			else
			{
				goodsDate.setPrice(product.getPrice());
				goodsDate.setRemaindNum(product.getTotalNum());
			}
			goodsDate.setPriceDisparity(0);
			goodsDate.setProductType(productType);
			goodsDate.setTheDate(new Timestamp(afterCalendar.getTimeInMillis()));//设置固定的时间为8：00：00
			System.out.println("时间为："+afterCalendar.getTime());
			addGoodsDateList.add(goodsDate);
			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);
		}
		if(addGoodsDateList.size() > 0)
		{
			goodsDateService.batchAddGoodsDates(addGoodsDateList);
		}
		Calendar date = Calendar.getInstance();
		date.add(Calendar.DAY_OF_YEAR,-1);
		List<GoodsDate> goodsDateInMouth = goodsDateService.queryGoodsDateInMouth(id,date.getTime());
		//查询未来30天每天的余票数
		return JSONResult.ok(goodsDateInMouth);
	}
	/**
	 * 获取未来一个月的剩余票
	 * @param id 景点/线路id或酒店中的商品id
	 * @param productType 0为景点;1为酒店;2为线路
	 * @return
	 */
	@PostMapping("/getNextMonthRemaidGood")
	@ResponseBody
	public JSONResult getNextMonthRemaidGood(String id,int productType)
	{
		if(StringUtils.isEmpty(id) || StringUtils.isEmpty(productType+""))
		{
			return JSONResult.errorMsg("请求参数不全.");
		}
		Product product = null;
		GoodsInfo info = null;
		//如果是酒店
		if(productType == 1)
		{
			info = goodsInfoService.getGoodsInfoById(id);
		}
		else if(productType == 0)
		{
			product = scenicHotelService.getScenicById(id);
		}
		else
		{
			product = travelLineService.getTravelLineById(id);
		}
		
		if(null == info && null == product) 
		{
			return JSONResult.errorMsg("获取商品信息失败");
		}
		
		Calendar date = Calendar.getInstance();
		date.add(Calendar.MONTH,1);
		date.set(Calendar.DAY_OF_MONTH, 1);
		date.set(Calendar.HOUR_OF_DAY, 0);
		date.set(Calendar.MINUTE, 0);
		date.set(Calendar.SECOND, 0);
		date.set(Calendar.MILLISECOND, 0);
		System.out.println(date.getTime());
		List<GoodsDate> goodsDateInNextMouth = goodsDateService.getNextMonthRemaidGood(id,date.getTime());
		//查询未来30天每天的余票数
		return JSONResult.ok(goodsDateInNextMouth);
	}
	/**
	 * 查询商品日期范围
	 * @param productId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@PostMapping("/getGoodsInfoBetweenDate")
	@ResponseBody
	public JSONResult getGoodsInfoBetweenDate(String productId,String startDate,String endDate){
		List<GoodsDate> list = goodsDateService.queryGoodsDateInDays(productId, startDate,endDate);
		return JSONResult.ok(list);
	}
	
	/**
	 * 更新某一天的售票信息
	 * @param goodsDate
	 * @return
	 */
	@RequestMapping("/updateGoodsDate")
	public JSONResult updateGoodsDate(GoodsDate goodsDate)
	{
		goodsDateService.updateGoodsDate(goodsDate);
		return JSONResult.ok();
	}
	/**
	 *  	批量更新商品详情
	 * @param goodsDateList
	 * @return
	 */
	@PostMapping("/batchUpdateGoodsDate")
	public JSONResult batchUpdateGoodsDate(@RequestBody List<GoodsDate> goodsDateList)
	{
		if(null == goodsDateList)
		{
			return JSONResult.errorMsg("参数不全");
		}
		goodsDateService.batchUpdateGoodsDate(goodsDateList);
		return JSONResult.ok();
}
}
